//
//  AccountModel.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/10.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import Foundation
import ObjectMapper

enum Gender: Int {
    case `default` = 0
    ///男性
    case male = 1
    ///女性
    case female = 2
    
    func descrip() -> String {
        switch self {
        case .male:
            return "男"
        case .female:
            return "女"
        default:
            return "未知"
        }
    }
}

class AccountModel: NSObject, Mappable, NSCoding  {
    var uid: String?
    var mobile: String?
    var token: String?
    ///所选择的考试级别
    var levelId: Int?
    var fullName: String?
    var gender: Gender?
    ///昵称
    var nickname: String?
    ///用户头像url
    var profilePhoto: String?
    ///毕业学校
    var graduated: String?
    ///学历
    var education: String?
    ///账户金币数量
    var goldCoin: Int?
    ///邮箱
    var email: String?
    
    func mapping(map: Map) {
        uid <- map["uid"]
        mobile <- map["mobile"]
        token <- map["token"]
        levelId <- map["levelId"]
        fullName <- map["fullName"]
        gender <- map["gender"]
        nickname <- map["nickname"]
        profilePhoto <- map["profilePhoto"]
        graduated <- map["graduated"]
        education <- map["education"]
        goldCoin <- map["goldCoin"]
        email <- map["email"]
    }
    required init?(map: Map) {
        
    }
    
    required override init() {
        
    }
    
    public func encode(with aCoder: NSCoder) {
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(mobile, forKey: "mobile")
        aCoder.encode(token, forKey: "token")
        aCoder.encode(fullName, forKey: "fullName")
        aCoder.encode(gender?.rawValue, forKey: "gender")
        aCoder.encode(nickname, forKey: "nickname")
        aCoder.encode(profilePhoto, forKey: "profilePhoto")
        aCoder.encode(graduated, forKey: "graduated")
        aCoder.encode(education, forKey: "education")
        aCoder.encode(email, forKey: "email")
        aCoder.encode(goldCoin, forKey: "goldCoin")
        aCoder.encode(levelId, forKey: "levelId")
    }
    
    required init?(coder aDecoder: NSCoder) {
        self.uid = aDecoder.decodeObject(forKey: "uid") as? String
        self.mobile = aDecoder.decodeObject(forKey: "mobile") as? String
        self.token = aDecoder.decodeObject(forKey: "token") as? String
        self.nickname = aDecoder.decodeObject(forKey: "nickname") as? String
        self.fullName = aDecoder.decodeObject(forKey: "fullName") as? String
        self.profilePhoto = aDecoder.decodeObject(forKey: "profilePhoto") as? String
        self.graduated = aDecoder.decodeObject(forKey: "graduated") as? String
        self.education = aDecoder.decodeObject(forKey: "education") as? String
        self.email = aDecoder.decodeObject(forKey: "email") as? String
        
        let coin = aDecoder.decodeObject(forKey: "goldCoin")
        self.goldCoin = coin as? Int
        let level = aDecoder.decodeObject(forKey: "levelId")
        self.levelId = level as? Int
        
        let gender = aDecoder.decodeObject(forKey: "gender")
        self.gender = gender as? Gender
        
    }
    
    ///清空用户信息
    public func clean() {
        uid = nil
        mobile = nil
        token = nil
        levelId = nil
        nickname = nil
        profilePhoto = nil
        graduated = nil
        education = nil
        goldCoin = nil
        gender = nil
        fullName = nil
        email = nil
    }
    
    ///更新
    public func update(_ account: AccountModel) {
        if account.uid != nil {
            uid = account.uid
        }
        
        if account.token != nil {
            token = account.token
        }
        if account.mobile != nil {
            mobile = account.mobile
        }
        if account.levelId != nil {
            levelId = account.levelId
        }
        if account.nickname != nil {
            nickname = account.nickname
        }
        if account.profilePhoto != nil {
            profilePhoto = account.profilePhoto
        }
        if account.graduated != nil {
            graduated = account.graduated
        }
        if account.education != nil {
            education = account.education
        }
        if account.goldCoin != nil {
            goldCoin = account.goldCoin
        }
        if account.gender != nil {
            gender = account.gender
        }
        if account.fullName != nil {
            fullName = account.fullName
        }
        if account.email != nil {
            email = account.email
        }
        
        
    }
    
}

